<!--交易中心的交易记录页面-->
<div class="layui-card">
    <div class="layui-card-header">
        <h2 class="header-title">交易记录</h2>
        <span class="layui-breadcrumb pull-right">
          <a href="#!console">首页</a>
          <a><cite>交易记录</cite></a>
        </span>
    </div>
    <div class="layui-card-body">
        <div class="layui-form toolbar">
            搜索：
            <input id="company" class="layui-input search-input" type="text" placeholder="公司名"/>&emsp;&emsp;
            <input id="position" class="layui-input search-input" type="text" placeholder="岗位名">&emsp;&emsp;
            <input type="text" id="start" lay-verify="date" placeholder="发布日期" autocomplete="off" class="layui-input search-input">&emsp;
            <input type="text" id="end" lay-verify="date" placeholder="截止日期" autocomplete="off" class="layui-input search-input">&emsp;
            <button id="records-btn-search" class="layui-btn icon-btn"><i class="layui-icon">&#xe615;</i>搜索</button>
            <!--            <button id="user-btn-add" class="layui-btn icon-btn"><i class="layui-icon">&#xe654;</i>添加</button>-->
        </div>

        <table class="layui-table" id="records-table" lay-filter="records-table"></table>
    </div>
</div>


<!-- 表格操作列 -->
<!--<a class="layui-btn layui-btn-primary layui-btn-xs" lay-event="edit">修改</a>-->
<script type="text/html" id="records-table-bar">

    <a class="layui-btn layui-btn-primary layui-btn-xs" lay-event="delete">删除</a>
</script>

<!-- 表格状态列 -->
<!--<script type="text/html" id="records-tpl-state">-->
<!--    <input type="checkbox" lay-filter="records-tpl-state" value="{{d.id}}" lay-skin="switch" lay-text="热招中|关闭"-->
<!--           {{d.conduct==0?'checked':''}}/>-->
<!--</script>-->

<script>
    layui.use(['form', 'table', 'util', 'config', 'admin','laydate'], function () {
        var form = layui.form;
        var table = layui.table;
        var config = layui.config;
        var layer = layui.layer;
        var util = layui.util;
        var admin = layui.admin;
        let laydate = layui.laydate;

        laydate.render({
            elem:"#start"
        })
        laydate.render({
            elem:"#end"
        })

        // 导出按钮绑定权限
        if (!admin.hasPerm('user:post/users/saveOrUpdate')) {
            $('#records-btn-export').hide();
        }


        // 渲染表格
        let recordsTable = table.render({
            elem: '#records-table',
            // url: config.base_server + 'user_query.json',
            url: config.base_server + 'api-trade/records/findAllRecords',
            method: 'GET',
            where: {
                access_token: config.getToken().access_token
            },
            page: true,
            //todo
            // 数据结果展示
            cols: [[
                {field: 'id', sort: true, title: '公司编号'},
                {field: 'companyname', sort: true, title: '公司名'},
                {field: 'remark', sort: true, title: '岗位名称'},
                {field: 'employeeCount', sort: true, title: '招聘人数'},
                {field: 'employedCount', sort: true, title: '录用人数'},
                {field: 'startTime', sort: true, title: '开始日期'},
                {field: 'startTime', sort: true, title: '截止日期'},
                // {field: 'conduct', sort: true, title: '录用人数'},
                // {sort: true, title: '是否在招聘',templet:function (d) {
                //         return d.conduct==0?"是":"否"
                //     }},

                {align: 'center', toolbar: '#records-table-bar', title: '操作', width: 150}
            ]]
        });
        // 添加按钮点击事件
        // $('#user-btn-add').click(function () {
        //     showEditModel();
        // });

        // 工具条点击事件
        table.on('tool(records-table)', function (obj) {
            console.log(obj);
            console.log("data是数据，event是类型");
            var data = obj.data;
            var layEvent = obj.event;
            //交易记录不存在修改
            if (layEvent === 'edit') { // 修改
                showEditModel(data);
            } else if (layEvent === 'reset') { // 重置密码
                layer.confirm('确定重置此用户的密码吗？', function (i) {
                    layer.close(i);
                    layer.load(2);
                    admin.req('api-user/users/' + obj.data.id + '/resetPassword', {}, function (data) {
                        layer.closeAll('loading');
                        if (data.code == 0) {
                            layer.msg(data.msg, {icon: 1, time: 500});
                        } else {
                            layer.msg(data.msg, {icon: 2, time: 500});
                        }
                    }, 'POST');
                });
            } else if (layEvent === 'delete') {
                doDelete(obj);//删除交易记录
            }
        });
        // 删除
        var doDelete = function (obj) {
            layer.confirm('确定要删除吗？', function (i) {
                layer.close(i);
                layer.load(2);
                admin.req('api-trade/records/deleteRecords/' + obj.data.id + '/' + obj.data.recordsId, {}, function (data) {
                    layer.closeAll('loading');
                    if (data.code == 0) {
                        layer.msg(data.msg, {icon: 1, time: 500});
                        obj.del();
                    } else {
                        layer.msg(data.msg, {icon: 2, time: 500});
                    }
                }, 'DELETE');
            });
        };
        //显示表单弹窗
        var showEditModel = function (data) {
            //修改sex值
            // if (data){
            //     data.sex = data.sex+'';
            // }
            admin.putTempData('t_records', data);//将数据放入缓存临时数据
            console.log(data);
            console.log("data是否有数据，有就是修改");
            var title = data ? '修改交易记录' : '添加';
            admin.popupCenter({
                title: title,
                path: 'pages/trade/records_edit.html',
                finish: function () {
                    table.reload('records-table', {});
                }
            });
        };

        // 搜索按钮点击事件
        //todo
        //当进行模糊查询时 调用findAllRecords方法，但是sql查询数据时条件没有
        $('#records-btn-search').click(function () {

            let params = {
                companyname:$("#company").val(),
                position:$("#position").val(),
                start:$("#start").val(),
                end:$("#end").val(),
                access_token: config.getToken().access_token
            }

            recordsTable.reload({
                where:params
            });
        });


        // 导出
        //todo
        //导不出来，存在数组越界
        $('#records-btn-export').click(function () {
            var url = config.base_server + 'api-trade/records/exportRecords?access_token=' + config.getToken().access_token;
            var xhr = new XMLHttpRequest();
            xhr.open('POST', url, true);
            xhr.responseType = "blob";
            xhr.setRequestHeader("client_type", "DESKTOP_WEB");
            xhr.onload = function () {
                if (this.status == 200) {
                    var blob = this.response;
                    var objecturl = URL.createObjectURL(blob);
                    window.location.href = objecturl;
                }
            }
            xhr.send();
        });

        // 修改user状态
        //由于招聘发布信息表在控制状态，记录不需要状态
        form.on('switch(records-tpl-state)', function (obj) {
            layer.load(2);
            admin.req('api-trade/records/updateEnabled', {
                id: obj.elem.value,
                recordsId: obj.elem.value,
                enabled: obj.elem.checked ? true : false
                //false 关闭 0 true 热招中 1
            }, function (data) {
                layer.closeAll('loading');
                if (data.code == 0) {
                    layer.msg(data.msg, {icon: 1, time: 500});
                    //table.reload('table-user', {});
                } else {
                    layer.msg(data.msg, {icon: 2, time: 500});
                    $(obj.elem).prop('checked', !obj.elem.checked);
                    form.render('checkbox');
                }
            }, 'GET');
        });
    });

    function myJsonAjax(url, data, type, callback) {
        let token = JSON.parse(layui.data('easyweb').token).access_token
        $.ajax({
            url: 'http://127.0.0.1:9200/'+url,
            data: JSON.stringify(data),
            dataType:"json",
            type: type,
            contentType: "application/json",
            beforeSend: function (xhr) {
                xhr.setRequestHeader('Authorization', 'Bearer ' + token);
            },
            success: callback
        })
    }
</script>